import cv2
import numpy as np

# 根据掩码生成bbox
def mask_to_bbox(mask):
    """
    input: mask, a binary mask
    output: bbox, a list of [x1, y1, x2, y2]
    """
    pts = np.where(mask != 0)
    #获取坐标的最大最小值
    min_x = np.min(pts[1])
    max_x = np.max(pts[1])
    min_y = np.min(pts[0])
    max_y = np.max(pts[0])
    data=[min_x,min_y,max_x,max_y]
    return data

def bbox_area(bbox):
    width = bbox[2] - bbox[0]
    height = bbox[3] - bbox[1]
    return width * height

